package Homework;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

public class MyCallable implements Callable<Integer> {
    private ArrayList<Integer> list;

    public MyCallable(ArrayList<Integer> list) {
        this.list = list;
    }

    @Override
    public Integer call() throws Exception {
        ArrayList<Integer> bokList = new ArrayList<>();
        int sum = 0;
        while (true) {
            synchronized (MyCallable.class) {
                if (list.isEmpty()) {
                    for (int i = 0; i < bokList.size(); i++) {
                        sum += bokList.get(i);
                    }
                    System.out.println(Thread.currentThread().getName() + " " + bokList + "，总金额为:" + sum + ",最大金额是：" + Collections.max(bokList));
                    break;
                } else {
                    // 继续抽奖
                    Collections.shuffle(list);
                    Integer prize = list.remove(0);
                    System.out.println(Thread.currentThread().getName() + " 又产生了一个:" + prize + "元大奖");
                    bokList.add(prize);
                }
                Thread.sleep(10);
            }
        }
        return sum;
    }
}